#!/bin/bash

source $DRAW_INI
source $DRAW_CFG

INPUT_STR=""

for f in $BAM_DIR/*.bam
do
 INPUT_STR=$INPUT_STR"-I $f "
done;

#optional dbsnp
if [ -z $SNPDB];then
  DTAG=""
else
  DTAG="-D $SNPDB"
fi

if [ -z $6];then
  LTAG=""
else
  LTAG="-L $6"
fi

java -Xmx4g -jar $GATK \
 -l INFO \
 -R $REF_FASTA \
 -T UnifiedGenotyper \
 -et NO_ET \
 -mbq $4 \
 --read_filter MappingQuality -mmq $5 \
 --genotype_likelihoods_model BOTH \
 -nt $GATK_THREADS \
 $DTAG \
 $LTAG \
 $INPUT_STR \
 -o ${3}/s_${1}${2}_mbq_${4}_mmq_${5}.vcf

EXITSTATUS=$?

if [ ! -s "${3}/s_${1}${2}_mbq_${4}_mmq_${5}.vcf" ]; then exit 100;fi;

if [ $(grep -cv "^#" "${3}/s_${1}${2}_mbq_${4}_mmq_${5}.vcf") -eq "0" ]
 then
  echo "problem, no SNPs!"
  exit 100;
fi

java -Xmx4g -jar $GATK \
 -l INFO \
 -R $REF_FASTA \
 -T SelectVariants \
 -selectType INDEL \
 --variant ${3}/s_${1}${2}_mbq_${4}_mmq_${5}.vcf \
 -o ${3}/s_${1}${2}_mbq_${4}_mmq_${5}_indel.vcf


if [ $EXITSTATUS -ne 0 ];then exit $EXITSTATUS;fi

